package com.example.demo.ela.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.ela.entiy.PartsEntiyEla;
import com.example.demo.ela.entiy.PartsEntiyS;
import com.example.demo.ela.repository.PartsEntiysRepository;

@RestController
@RequestMapping(value = "/es/parts")
public class EsPartsController {

	@Autowired
	PartsEntiysRepository partsEntiysRepository;
	@RequestMapping(value = "/add")
	public PartsEntiyS add(@RequestParam Long id,@RequestParam String name) {
        
		//创建类
		PartsEntiyS detailOrderDocument =new PartsEntiyS();
		
		detailOrderDocument.setId(id);
		
		detailOrderDocument.setName(name);
	
		
		PartsEntiyS save = partsEntiysRepository.save(detailOrderDocument);
		
		return save;

	}
	
	@RequestMapping(value = "/search")
  public  List<PartsEntiyS> a(String searchContent ) {
	  QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
      System.out.println("查询的语句:"+builder);
      Iterable<PartsEntiyS> search = partsEntiysRepository.search(builder);
     Iterator<PartsEntiyS> iterator = search.iterator();
      List<PartsEntiyS> list=new ArrayList<PartsEntiyS>();
      while (iterator.hasNext()) {
        list.add(iterator.next());
      }
	  return list;
  }
	
	
	
}
